﻿using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.Builders;
using MongoDB.Driver.Linq;
using SmallJob.Model;
using SmallJob.Services.Implements;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;

namespace SmallShop.Services.Code
{
    public class ToMongoDB
    {
        public static MongoDatabase openMongoDB()
        {
            var connectionString = ConfigurationManager.AppSettings["mongourl"];
            var client = new MongoClient(connectionString);
            return client.GetServer().GetDatabase(ConfigurationManager.AppSettings["mongodatabase"]);
        }
        public MongoDatabase db { get; set; }
        public ToMongoDB()
        {
            db = ToMongoDB.openMongoDB();
        }
        public List<MMessage> ToMongoMessage(List<Message> originmes, MConversation cvs)
        {
            List<MMessage> newlst = new List<MMessage>();
            foreach (var item in originmes)
            {
                var newMMes = new MMessage();
                newMMes.Id = ObjectId.GenerateNewId();
                newMMes.SenderID = item.SenderID;
                newMMes.ReceiverID = item.ReceiveID;
                newMMes.IsRead = item.IsRead;
                newMMes.Content = item.Content;
                newMMes.Date = item.Date;
                newMMes.ParentID = cvs.ShortID;
                newMMes.Type = cvs.Type;
                newlst.Add(newMMes);
            }
            newlst = newlst.OrderBy(m => m.Date).ToList();
            SaveMMessage(newlst);
            return newlst;
        }
        public void SaveMMessage(List<MMessage> newmes)
        {
            var col = db.GetCollection<MMessage>("messages");
            foreach (var item in newmes)
            {
                col.Save(item);
            }
        }
        public void ClearCollection()
        {
            db.GetCollection("conversations").RemoveAll();
            db.GetCollection("messages").RemoveAll();
        }
        public void AddMConversation(MConversation cvs)
        {
            db.GetCollection("conversations").Save(cvs);
        }

        public MUser ConvertUser(User user)
        {
            MUser mu = new MUser();
            mu.ID = user.ID;
            mu.Name = user.ProperName;
            mu.UserName = user.UserName;
            mu.Avatar = user.Avatar;
            return mu;
        }
        public MOrder ConvertOrder(Order order)
        {
            MOrder mo = new MOrder();
            mo.OrderID = order.ID;
            mo.OrderType = order.Type;
            mo.SellerID = order.SellerID;
            if ((order.Type == (int)OrderType.PROVIDE) && ((!order.Completed || order.Comment == null || order.Comment.Length <= 0) && !order.IsDeleted))
            {
                mo.IsNoDisplay = false;
            }
            else
            {
                mo.IsNoDisplay = true;
            }
            return mo;
        }
        public MProvide ConvertProvide(Provide provide)
        {
            MProvide mp = new MProvide();
            mp.ID = provide.ID;
            mp.Title = provide.Title;
            mp.HostID = provide.UserID;
            return mp;
        }
        public MRequest ConvertRequest(Request provide)
        {
            MRequest mp = new MRequest();
            mp.ID = provide.ID;
            mp.Title = provide.Title;
            mp.HostID = provide.AssignedUserID;
            return mp;
        }

        public object GetListConversations()
        {
            var lst = db.GetCollection<MConversation>("conversations").FindAll().ToList();
            var colmes = db.GetCollection<MMessage>("messages");
            foreach (var item in lst)
            {
                var query = Query<MMessage>.EQ(m => m.ParentID, item.ShortID);
                item.Messages = colmes.Find(query).SetSortOrder(SortBy<MMessage>.Descending(m => m.Date)).SetLimit(20).ToList();
            }
            return lst;
        }

        public void FixConversation()
        {
            var query = Query<MConversation>.Exists(c => c.Order);
            List<MConversation> lst = db.GetCollection<MConversation>("conversations").Find(query).ToList();
            foreach (var item in lst)
            {
                if (item.Order != null)
                {
                    MProvide provide = item.Order.Provide;
                    MRequest request = item.Order.Request;
                    item.Order.Provide = null;
                    item.Order.Request = null;
                    db.GetCollection<MConversation>("conversations").Save(item);
                    item.Provide = provide;
                    item.Request = request;
                    db.GetCollection<MConversation>("conversations").Save(item);
                }
                
            }
        }
    }
}
